package com.yjs.app.trade.web.controller.base.util;

import javax.validation.ConstraintViolationException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yjs.app.trade.core.base.exception.Error;

/**
 * 基础controller
 * 
 * @author hikaru
 * @since 1.0.0
 */
public class ExceptionInterceptorController {
	
	/**
	 * 日志
	 */
	protected Logger logger = LogManager.getLogger(getClass());
	
	/**
	 * 处理验证异常
	 */
	@ExceptionHandler(MethodArgumentNotValidException.class)
	@ResponseBody
	public Result handleValidation(MethodArgumentNotValidException e) {
		return Result.fail(e.getBindingResult());
	}
	
	/**
	 * 处理验证异常
	 */
	@ExceptionHandler(ConstraintViolationException.class)
	@ResponseBody
	public Result handleValidation(ConstraintViolationException constraintViolationException) {
		return Result.fail(constraintViolationException.getConstraintViolations());
	}
	
	/**
	 * 处理未知异常
	 */
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public Object handleException(Exception e) {
		e.printStackTrace();
		logger.error(e);
		return Result.cfail(new Error(e));
	}
	
}
